package com.itheima.service.mongo.impl;

import com.itheima.domain.mongo.Voice;
import com.itheima.service.mongo.PeachBlossomService;
import com.itheima.util.ConstantUtil;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service
public class PeachBlossomServiceImpl implements PeachBlossomService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private IdService idService;

    /**
     * 1.保存语音
     * @param voice
     */
    @Override
    public void saveVoice(Voice voice) {

        voice.setCreate(System.currentTimeMillis());
        voice.setPid(idService.getNextId(ConstantUtil.VOICE_ID));
        mongoTemplate.save(voice);
    }

    /**
     * 2.查询所有语音 返回总个数
     * @return
     */
    @Override
    public List<Voice> findAllVoice() {
        Query query = new Query();
       return mongoTemplate.find(query, Voice.class);

    }

    /**
     * 3.根据pid查询语音
     */
    @Override
    public Voice findVoiceByPid(long pid) {
        Query query = new Query(
                Criteria.where("pid").is(pid)
        );
        Voice voice = mongoTemplate.findOne(query, Voice.class);
        return voice;
    }

    /**
     * 4.根据pid删除语音
     */
    @Override
    public void deleteVoiceByPid(long pid) {
        Query query = new Query(
                Criteria.where("pid").is(pid)
        );
        mongoTemplate.remove(query, Voice.class);
    }

    /**
     * 5.根据userId查询语音
     */
   /* @Override
    public List<Voice> findVoiceByUserId(long userId) {
        Query query = new Query(
                Criteria.where("userId").is(userId)
        );
        List<Voice> voiceList = mongoTemplate.find(query, Voice.class);
        return voiceList;
    }*/

}
